//
//  GroupMsgSender.m
//  SportSocial
//
//  Created by yull on 15/1/22.
//  Copyright (c) 2015年 yull. All rights reserved.
//

#import "GroupMsgSender.h"
#import "GroupChatMsg.h"
#import "GroupIMNet.h"
#import "CurrentChat.h"
#import "GroupMsgDao.h"
#import "GroupDetailViewController.h"

@implementation GroupMsgSender

- (instancetype)initWithGroup:(NearGroup *)group
{
    if (self = [super init]) {
        self.chatGroup = group;
        self.name = group.name;
        [CurrentChat shareInstance].chatGroup = group;
    }
    return self;
}

- (void)addCommonFieldWithChatMsg:(GroupChatMsg *)groupMsg
{
    groupMsg.sendId = [CurrentUser shareInstance].userId;
    groupMsg.senderName = [CurrentUser shareInstance].name;
    groupMsg.createTime = [[NSDate date] timeIntervalSince1970] * 1000;
    groupMsg.roleType = ChatMsgRoleTypeSend;
    groupMsg.isRead = YES;
    groupMsg.isAck = YES;
    groupMsg.sendStatus = ChatMsgSendStatusSending;
    groupMsg.retainField = @"";
    groupMsg.picUploadPercent = 0.0f;
    groupMsg.groupIcon = self.chatGroup.icon;
    groupMsg.groupId = self.chatGroup.group_id;
    groupMsg.groupName = self.chatGroup.name;
}

- (void)createMsgWithText:(NSString *)text
{
    GroupChatMsg *groupMsg = [[GroupChatMsg alloc] init];
    groupMsg.content = text;
    groupMsg.msgType = ChatMsgTypeText;
    groupMsg.msgSubType = ChatMsgSubTypeText;
    groupMsg.decodeContent = text;
    groupMsg.msgLength = 0;
    
    [self addCommonFieldWithChatMsg:groupMsg];
    
    [[GroupIMNet shareInstance] sendGroupTextMsg:groupMsg isResend:NO];
}

- (void)createMsgWithAudioData:(NSData *)audioData
                   audioLength:(NSInteger)audioLength
                          path:(NSString *)filePath
{
    GroupChatMsg *groupMsg = [[GroupChatMsg alloc] init];
    groupMsg.content = @"语音";
    groupMsg.msgType = ChatMsgTypeAudio;
    groupMsg.msgSubType = ChatMsgSubTypeAudio;
    groupMsg.decodeContent = groupMsg.content;
    groupMsg.msgLength = audioLength;
    groupMsg.audioRestTime = audioLength;
    
    [self addCommonFieldWithChatMsg:groupMsg];
    
    //groupMsg.localPath = [FunctionMethod createAudioPath];
    //BOOL ret = [audioData writeToFile:groupMsg.localPath atomically:YES];
    //if (ret) {
    groupMsg.localPath = filePath;
        NSLog(@"保存成功!");
        groupMsg.audioStatus = ChatMsgAudioStatusNone;
        
        [[GroupIMNet shareInstance] sendGroupAudioMsg:groupMsg audioData:audioData isResend:NO];
    //}
}

- (void)createMsgWithImage:(UIImage *)image
{
    GroupChatMsg *groupMsg = [[GroupChatMsg alloc] init];
    groupMsg.content = @"图片";
    groupMsg.msgType = ChatMsgTypePic;
    groupMsg.msgSubType = ChatMsgSubTypePic;
    groupMsg.decodeContent = groupMsg.content;
    groupMsg.msgLength = 0;
    
    [self addCommonFieldWithChatMsg:groupMsg];
    
    NSData *imageData = UIImageJPEGRepresentation(image, 0.1f);
    
    groupMsg.localPath = [FunctionMethod createPicPath];
    BOOL ret = [imageData writeToFile:groupMsg.localPath atomically:YES];
    if (ret) {
        NSLog(@"保存成功!");
        [[GroupIMNet shareInstance] sendGroupPicMsg:groupMsg imageData:imageData isResend:NO];
    }
}

- (NSArray *)selectChatMsg
{
    return [[GroupMsgDao shareInstance] selectAllGroupMsgArrayWithGroupId:self.chatGroup.group_id];
}

- (NSArray *)selectHistoryMsgWithDbId:(UInt64)dbId
{
    return [[GroupMsgDao shareInstance] selectHistoryGroupMsgArrayWithGroupId:self.chatGroup.group_id dbId:dbId];
}

- (BOOL)updateReadWithMsgId:(UInt64)msgId
{
    return [[GroupMsgDao shareInstance] updateReadWithDBId:msgId];
}

- (NSArray *)selectAllPicMsg
{
    return [[GroupMsgDao shareInstance] selectAllPicMsgWithGroupId:self.chatGroup.group_id];
}

- (UIViewController *)createDetailViewController
{
    GroupDetailViewController *detailViewController = [[GroupDetailViewController alloc] init];
    detailViewController.groupId = self.chatGroup.group_id;
    return detailViewController;
}

- (void)resendMsg:(ChatBaseMsg *)msg
{
    msg.sendStatus = ChatMsgSendStatusSending;
    
    GroupChatMsg *chatMsg = (GroupChatMsg *)msg;
    
    if (msg.msgType == ChatMsgTypeText) {
        [[GroupIMNet shareInstance] sendGroupTextMsg:chatMsg isResend:YES];
    }else if (msg.msgType == ChatMsgTypePic) {
        NSData *imgData = [NSData dataWithContentsOfFile:msg.localPath];
        [[GroupIMNet shareInstance] sendGroupPicMsg:chatMsg imageData:imgData isResend:YES];
    }else {
        NSData *audioData = [NSData dataWithContentsOfFile:msg.localPath];
        [[GroupIMNet shareInstance] sendGroupAudioMsg:chatMsg audioData:audioData isResend:YES];
    }
}
@end
